<?php
/**
 * 导航
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class Nav extends Model
{
    //set talbename
    protected $table = 'nav';
    //set primary key
    protected $pk = 'nav_id';

    /**
     * @desc 添加导航
     * @param $param
     * @return false|int
     */
    public function addNav($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    public function updateNav($nav_id, $param)
    {
        $conds = array(
            'nav_id' => $nav_id,
        );
        $field = array();
        if (isset($param['name'])) {
            $field['name'] = $param['name'];
        }

        if (isset($param['is_show'])) {
            $field['is_show'] = $param['is_show'];
        }

        if (isset($param['image'])) {
            $field['image'] = $param['image'];
        }

        if (isset($param['vieworder'])) {
            $field['vieworder'] = $param['vieworder'];
        }

        if (isset($param['url'])) {
            $field['url'] = $param['url'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    public function deleteNav($nav_id)
    {
        $conds = array(
            'nav_id' => $nav_id,
        );
        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * @desc 获取导航
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getNav()
    {
        $field = '*';
        return Db::table($this->table)->field($field)->select();
    }
}
